import Vue from 'vue'
import VueRouter from 'vue-router'
import 'element-ui/lib/theme-chalk/index.css'
import Menu from '../components/user/UserMenu.vue'
import Home from '@/components/user/UserHome'
import Login from '@/views/Login'
import Task from '@/components/user/work/Task'
import WorkLog from '@/components/user/work/WorkLog'
import Attendance from '@/components/user/oaApprove/Attendance'
import Business from '@/components/user/oaApprove/Business'
import LeaveJob from '@/components/user/oaApprove/LeaveJob'
import UserInfo from '@/components/user/userInfo/UserInfo'
import Schedule from '@/components/user/work/Schedule'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Login
  }, {
    path: '/user',
    component: Menu,
    redirect: '/user/home',
    children: [
      {
        path: '/user/home',
        component: Home
      }, {
        path: '/user/task',
        component: Task
      }, {
        path: '/user/log',
        component: WorkLog
      }, {
        path: '/user/schedule',
        component: Schedule
      }, {
        path: '/user/attendance',
        component: Attendance
      }, {
        path: '/user/business',
        component: Business
      }, {
        path: '/user/job',
        component: LeaveJob
      }, {
        path: '/user/info',
        component: UserInfo
      }, {
        path: '/user/changePassword',
        component: () => import('../components/user/userInfo/ChangePassword')
      }
    ]
  }, {
    path: '/admin',
    component: () => import('../components/admin/AdminMenu'),
    redirect: '/admin/Home',
    children: [
      {
        path: '/admin/Home',
        component: () => import('../components/admin/AdminHome')
      }, {
        path: '/admin/userManger',
        component: () => import('../components/admin/UserManger')
      }, {
        path: '/admin/depManger',
        component: () => import('../components/admin/DepManger')
      }, {
        path: '/admin/userManger/addEmp',
        component: () => import('../components/admin/AddEmpInfo')
      }, {
        path: '/admin/interfaceDoc',
        component: () => import('../components/admin/InterfaceDoc')
      }
    ]
  }, {
    path: '/about',
    component: () => import('../views/About.vue')
  }, {
    path: '/exec',
    component: () => import('../components/exec/Input')
  }
]

const router = new VueRouter({
  routes
})

export default router

router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    return next()
  }

  const token = window.sessionStorage.getItem('userToken')
  const token1 = window.sessionStorage.getItem('adminToken')
  // if (token !== null && token.length > 0) 等同于 if (token) 表示token不为null为true反之为false
  if (token !== null || token1 !== null) {
    return next()
  } else {
    alert("请登录")
    return next('/')
  }
})
